//
//  MSBHUDManager.swift
//  DaDaClass
//
//  Created by problemchild on 2018/4/2.
//  Copyright © 2018年 dadaabc. All rights reserved.
//

import Foundation
import SVProgressHUD

@objc public extension MSBHUDManager {

    /// 恢复默认设置
    class func defaultConfig() {
        defaultHUDManager.defaultConfig()
    }

    /// 更改相关设置，（传入自定义配置代码）（使代码更清晰）
    class func changeConfig(with block: @escaping () -> Void) {
        defaultHUDManager.changeConfig(with: block)
    }

    /// 展示成功HUD
    ///
    /// - Parameters:
    ///   - message: 文字内容
    ///   - maxDelay: 最长展示时间(可选)（不能大于整个config中配置的时长）
    class func showHUD(success message: String,
                       maxDelay: TimeInterval = 0) {
        MSBHUDManager.showHUD(with: message, type: .Success,
                             maxDelay: maxDelay)
    }

    /// 展示感叹号HUD
    ///
    /// - Parameters:
    ///   - message: 文字内容
    ///   - maxDelay: 最长展示时间(可选)（不能大于整个config中配置的时长）
    class func showHUD(info message: String,
                       maxDelay: TimeInterval = 0) {
        MSBHUDManager.showHUD(with: message, type: .Info,
                             maxDelay: maxDelay)
    }

    /// 展示错误HUD
    ///
    /// - Parameters:
    ///   - message: 文字内容
    ///   - maxDelay: 最长展示时间(可选)（不能大于整个config中配置的时长）
    class func showHUD(error message: String,
                       maxDelay: TimeInterval = 0) {
        MSBHUDManager.showHUD(with: message, type: .Error,
                             maxDelay: maxDelay)
    }

    /// 展示处理中HUD
    ///
    /// - Parameters:
    ///   - message: 文字内容
    ///   - maxDelay: 最长展示时间(可选)（不能大于整个config中配置的时长）
    class func showHUD(processing message: String,
                       maxDelay: TimeInterval = 0) {
        MSBHUDManager.showHUD(with: message, type: .Processing,
                             maxDelay: maxDelay)
    }

    /// 展示成功、信息、错误、仅文字的HUD
    ///
    /// - Parameters:
    ///   - message: 文字内容
    ///   - type: .Success,.Info,.Error,.TextOnly
    ///   - maxDelay: 最长展示时间(可选)（不能大于整个config中配置的时长）
    class func showHUD(with message: String,
                       type: BSHUDType = BSHUDType.Success,
                       maxDelay: TimeInterval = 0) {
        defaultHUDManager.showHUD(with: message, type: type, maxDelay: maxDelay)
    }

    /// 展示带进度的HUD
    ///
    /// - Parameters:
    ///   - message: 文字内容
    ///   - progress: 进度
    ///   - maxDelay: 最长展示时间(可选)（不能大于整个config中配置的时长）
    class func showHUD(with message: String,
                       progress: Float,
                       maxDelay: TimeInterval = 0) {
        defaultHUDManager.showHUD(with: message, progress: progress, maxDelay: maxDelay)
    }

    /// 展示带图片的HUD
    ///
    /// - Parameters:
    ///   - message: 文字内容
    ///   - image: 图片
    ///   - maxDelay: 最长展示时间(可选)（不能大于整个config中配置的时长）
    class func showHUD(with message: String,
                       image: UIImage,
                       maxDelay: TimeInterval = 0) {
        defaultHUDManager.showHUD(with: message, image: image, maxDelay: maxDelay)
    }

    /// 展示HUD并让其处于处理中的状态
    ///
    /// - Parameters:
    ///   - maxDelay: 最长展示时间(可选)（不能大于整个config中配置的时长）
    class func showHUDinProcessing(maxDelay: TimeInterval = 0) {
        defaultHUDManager.showHUDinProcessing(maxDelay: maxDelay)
    }

    /// 让HUD消失
    ///
    /// - Parameters:
    ///   - sec: 延迟多少秒再消失（可选）
    class func dismiss(after sec: TimeInterval = 0) {
        defaultHUDManager.dismiss(after: sec)
    }
    
    //得到延迟回调
    class func dismiss(after sec: TimeInterval = 0,block:@escaping()->Void) {
        defaultHUDManager.dismiss(after: sec) {
            block()
        }
    }

    /// 方便存储一个时间，例如让aWhile代表1.5秒(默认)
    class func aWhile() -> TimeInterval {
        return defaultHUDManager._aWhile
    }

    /// aWhile是方便存储一个时间，例如让aWhile代表1.5秒(默认)
    ///
    /// - Parameters:
    ///   - time: 把aWhile设置成多少
    class func setAWhile(_ time: TimeInterval) {
        defaultHUDManager.setAWhileTime(time)
    }

    /// 一会儿（可配置）让HUD消失
    class func dismissAfterAWhile() {
        defaultHUDManager.dismissAfterAWhile()
    }
    
    // 修改弹框时候时候交互模式
//    class func changeInteractiveModel(maskType:SVProgressHUDMaskType) {
//        SVProgressHUD.setDefaultMaskType(maskType)
//    }

    static let defaultHUDManager = MSBHUDManager()

}

@objc public enum BSHUDType: NSInteger {
    case Success, Info, Error, Processing
    case AnimateProcessing, AnimateSuccess
}

public class MSBHUDManager: NSObject {

    @objc override init() {
        super.init()
        defaultConfig()
    }

    var _aWhile: TimeInterval = 1.5

    var dissmissViewBlock : (()->Void)?
    
    @objc public func setAWhileTime(_ time: TimeInterval) {
        _aWhile = time
    }

    @objc public func getAWhileTime() -> TimeInterval {
        return _aWhile
    }

    @objc public func defaultConfig() {
        DispatchQueue.main.async(execute: {
//            SVProgressHUD.setForegroundColor(UIColor.black)
//            SVProgressHUD.setBackgroundColor(UIColor.white)
//            SVProgressHUD.setDefaultStyle(.custom)
            SVProgressHUD.setDefaultMaskType(.none)
//            SVProgressHUD.setBackgroundLayerColor(.init(white: 0, alpha: 0.2))
            SVProgressHUD.setMinimumDismissTimeInterval(2)
            SVProgressHUD.setMaximumDismissTimeInterval(3)
//            SVProgressHUD.setImageViewSize(CGSize(width: 0, height: -12))
            SVProgressHUD.setCornerRadius(5)
            SVProgressHUD.setDefaultStyle(.dark)
            SVProgressHUD.setFont(.systemFont(ofSize: 14, weight: .medium))
            let bgcl = UIColor.init(red: 0x0E/255, green: 0x0F/255, blue: 0x23/255, alpha: 1)
            SVProgressHUD.setBackgroundColor(bgcl)
            SVProgressHUD.setForegroundColor(.white)
            SVProgressHUD.perform(#selector(SVProgressHUD.setInfoImage(_:)), with: nil)
        })
    }

    @objc public func changeConfig(with block:@escaping () -> Void) {
        DispatchQueue.main.async(execute: {
            block()
        })
    }

    @objc public func showHUD(with message: String,
                              type: BSHUDType = BSHUDType.Success,
                              maxDelay: TimeInterval = 0) {
        if message.length <= 0 { return }

        DispatchQueue.main.async(execute: {
            switch type {
            case .Success:
                SVProgressHUD.showSuccess(withStatus: message)
            case .Info:
                SVProgressHUD.showInfo(withStatus: message)
            case .Error:
                SVProgressHUD.showError(withStatus: message)
            case .Processing:
                SVProgressHUD.show(withStatus: message)
            case .AnimateProcessing:
                SVProgressHUD.showError(withStatus: message)
//                BearLoadingAnimationView.show()
            case .AnimateSuccess:
                SVProgressHUD.showError(withStatus: message)
//                BearSuccessAnimationView.show()
            }
        })
        if maxDelay > 0 {
            self.dismiss(after: maxDelay)
        }
    }

    @objc public func showHUD(with message: String,
                              progress: Float,
                              maxDelay: TimeInterval = 0) {
        DispatchQueue.main.async(execute: {
            SVProgressHUD.showProgress(progress, status: message)
        })
        if maxDelay > 0 {
            self.dismiss(after: maxDelay)
        }
    }

    @objc public func showHUD(with message: String,
                              image: UIImage,
                              maxDelay: TimeInterval = 0) {
        DispatchQueue.main.async(execute: {
            SVProgressHUD.show(image, status: message)
        })
        if maxDelay > 0 {
            self.dismiss(after: maxDelay)
        }
    }

    @objc public func showHUDinProcessing(maxDelay: TimeInterval = 0) {
        DispatchQueue.main.async(execute: {
            SVProgressHUD.show()
        })
        if maxDelay > 0 {
            self.dismiss(after: maxDelay)
        }
    }

    @objc public func dismiss(after sec: TimeInterval = 0) {
        DispatchQueue.main.async(execute: {
            SVProgressHUD.dismiss(withDelay: sec)
//            BearHudLoadingView.dismiss()
//            BearSuccessAnimationView.dismiss()
            DispatchQueue.main.asyncAfter(deadline: .now() + sec, execute: {
                self.dissmissViewBlock?()
//                BearLoadingAnimationView.dismiss()
            })
        })
    }
    
    public func dismiss(after sec: TimeInterval = 0,block:@escaping() -> Void) {
        DispatchQueue.main.async(execute: {
            SVProgressHUD.dismiss(withDelay: sec)
            DispatchQueue.main.asyncAfter(deadline: .now() + sec, execute: {
                block()
            })
        })
    }

    @objc public func dismissAfterAWhile() {
        dismiss(after: _aWhile)
    }
}
